package com.dy.design_pattern_study.abstract_factory_pattern.dome_abstract_factory.dome_simple_factory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @CreateBy zhangdingyi
 * @Date 2023-04-05
 * @Content 读取xml数据
 */
public class ReadXmlData {
    private static final String urlData = "com.dy.design_pattern_study." +
            "abstract_factory_pattern.dome_abstract_factory.IDB.impl.";

    public static void main(String[] args) throws Exception {
        readXml();
    }

    public static List<String> readXml() {
        List<String> urls = null;
        try {
            //文件路径
            String url = "D:\\working_space\\designPattern\\design_pattern_study\\" +
                    "src\\main\\resources\\readDBClass.xml";

            //声明一个类来存储读取的xml url
            urls = new ArrayList<>();
            //1-将文件转换io流
            InputStream inputStream = new FileInputStream(url);
            /** javax.xml.parsers 包中的DocumentBuilderFactory用于创建DOM模式的解析器对象 ，
             * DocumentBuilderFactory是一个抽象工厂类，它不能直接实例化，但该类提供了一个newInstance方法 ，
             * 这个方法会根据本地平台默认安装的解析器，自动创建一个工厂的对象并返回。
             */
            //2-调用 DocumentBuilderFactory.newInstance() 方法得到创建 DOM 解析器的工厂
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            //3-调用工厂对象的 newDocumentBuilder方法得到 DOM 解析器对象。
            DocumentBuilder docBuilder = factory.newDocumentBuilder();
            //4-调用 DOM 解析器对象的 parse() 方法解析 XML 文档，得到代表整个文档的 Document 对象，进行可以利用DOM特性对整个XML文档进行操作了。
            Document doc = docBuilder.parse(inputStream);
            //5-得到 XML 文档的根节点
            Element root = doc.getDocumentElement();
            //6-关闭流
            inputStream.close();
            //System.out.println("is ok");
            //获取所有指定名称的子标签
            NodeList nodeList = root.getElementsByTagName("factory");
//        //获取所有子标签
//        NodeList nodeList = root.getChildNodes();
            //遍历
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);
                if (node instanceof Element) {
                    Element element = (Element) node;
                    //获取国家信息
                    String countryname = element.getAttribute("url");
                    //System.out.println("countryname:====" + countryname);
                    urls.add(urlData + countryname);
                    //只获取中国
                   /* if (countryname.equals("中国")) {
                        //获取下一级所有子标签
                        NodeList n2 = element.getChildNodes();
                        System.out.println("NodeList长度，猜测是节点+孩子的总数："+n2.getLength());
                        //总省份数
                        int count = 0;
                        //遍历省份信息
                        for (int j = 0; j < n2.getLength(); j++) {
                            Node node2 = n2.item(j);
                            if (node2 instanceof Element) {
                                count++;
                                Element e2 = (Element) node2;
                                //获取省份信息
                                String provincename = e2.getAttribute("Name");
                                String code = e2.getAttribute("Code");
                                System.out.println(provincename + "==" + code);
                                //
                                NodeList city = e2.getElementsByTagName("City");
                                for (int k = 0; k < city.getLength(); k++) {
                                    Node node3 = city.item(k);
                                    if (node3 instanceof Element){
                                        Element c = (Element) node3;
                                        //获取城市信息
                                        String cityname = c.getAttribute("Name");
                                        System.out.println("##"+cityname);
                                        //
                                        NodeList region = c.getElementsByTagName("Region");
                                        for (int m = 0; m < region.getLength(); m++) {
                                            Node node4 = region.item(m);
                                            if (node4 instanceof Element){
                                                Element r = (Element)node4;
                                                String regionname = r.getAttribute("Name");
                                                System.out.println("#####"+regionname);
                                            }

                                        }

                                    }
                                }
                            }
                        }
                        System.out.println("总省份数目："+count);

                    }*/
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return urls;
    }

}
